package leetcode_day._2022._202209._2130;

import java.util.Arrays;

/**
 * 1652. 拆炸弹
 * 算法：前缀和、滑动窗口
 *
 * @author yzh
 * @version 1.0
 * @date 2022/9/24 11:42
 */
public class _24_1652 {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(new _24_1652().decrypt(new int[]{5, 7, 1, 4}, 3)));
    }

    public int[] decrypt(int[] code, int k) {
        int n = code.length;
        int[] ans = new int[n];
        if (k == 0) return ans;
        int[] sum = new int[2 * n + 10];
        for (int i = 1; i <= 2 * n; i++) sum[i] = sum[i - 1] + code[(i - 1) % n];
        for (int i = 1; i <= n; i++) {
            if (k < 0) ans[i - 1] = sum[i + n - 1] - sum[i + n + k - 1];
            else ans[i - 1] = sum[i + k] - sum[i];
        }
        return ans;
    }

    public int[] decryptDoublePoint(int[] code, int k) {
        int n = code.length;
        int[] ans = new int[n];
        if (k == 0) return ans;
        int[] newCode = new int[2 * n];
        System.arraycopy(code, 0, newCode, 0, n);
        System.arraycopy(code, 0, newCode, n, n);
        code = newCode;
        int l = k > 0 ? 1 : n + k;
        int r = k > 0 ? k : n - 1;
        int w = 0;
        for (int i = l; i <= r; i++) w += code[i];
        for (int i = 0; i < n; i++) {
            ans[i] = w;
            w -= code[l];
            w += code[r + 1];
            ++l;
            ++r;
        }
        return ans;
    }

}
